<?php
class AssignmentsController extends AppController {

	var $name = 'Assignments';
	var $helpers = array('Html', 'Form' );
	var $uses = array('Assignment', 'Resident', 'Semester', 'SemesterApartement');

	function index() {
		$this->Assignment->recursive = 2;
		$this->set('assignments', $this->Assignment->findAll());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Assignment.');
			$this->redirect('/assignments/index');
		}
		$this->set('assignment', $this->Assignment->read(null, $id));
	}

	function chooseSemester() {
		if (empty($this->data)) {
			$this->set('semesterList', $this->Semester->getSemesterList());
			return;
		}

		$semesterId = $this->data['Assignment']['semester_id'];
		$this->redirect("assignments/add/$semesterId");
	}

	function add($semesterId = null) {
		if ($semesterId == null) {
			$this->redirect('assignments/chooseSemester');
			return;
		}
		$this->set('residentList', $this->Resident->getResidentList());
		$this->set('semesterApartementList', $this->SemesterApartement->getSemesterApartementList($semesterId));
		$this->set('semesterId', $semesterId);
		if (empty($this->data)) {
			$this->render();
		} else {
			$this->cleanUpFields();
			if ($this->Assignment->save($this->data)) {
				$this->Session->setFlash('The Assignment has been saved');
				$this->redirect('/assignments/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function edit($id = null) {
		if (empty($this->data)) {
			if (!$id) {
				$this->Session->setFlash('Invalid id for Assignment');
				$this->redirect('/assignments/index');
			}
			$this->data = $this->Assignment->read(null, $id);
		} else {
			$this->cleanUpFields();
			if ($this->Assignment->save($this->data)) {
				$this->Session->setFlash('The Assignment has been saved');
				$this->redirect('/assignments/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Assignment');
			$this->redirect('/assignments/index');
		}
		if ($this->Assignment->del($id)) {
			$this->Session->setFlash('The Assignment deleted: id '.$id.'');
			$this->redirect('/assignments/index');
		}
	}


	function admin_index() {
		$this->Assignment->recursive = 0;
		$this->set('assignments', $this->Assignment->findAll());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Assignment.');
			$this->redirect('/admin/assignments/index');
		}
		$this->set('assignment', $this->Assignment->read(null, $id));
	}

	function admin_add() {
		if (empty($this->data)) {
			$this->render();
		} else {
			$this->cleanUpFields();
			if ($this->Assignment->save($this->data)) {
				$this->Session->setFlash('The Assignment has been saved');
				$this->redirect('/admin/assignments/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function admin_edit($id = null) {
		if (empty($this->data)) {
			if (!$id) {
				$this->Session->setFlash('Invalid id for Assignment');
				$this->redirect('/admin/assignments/index');
			}
			$this->data = $this->Assignment->read(null, $id);
		} else {
			$this->cleanUpFields();
			if ($this->Assignment->save($this->data)) {
				$this->Session->setFlash('The Assignment has been saved');
				$this->redirect('/admin/assignments/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Assignment');
			$this->redirect('/admin/assignments/index');
		}
		if ($this->Assignment->del($id)) {
			$this->Session->setFlash('The Assignment deleted: id '.$id.'');
			$this->redirect('/admin/assignments/index');
		}
	}

}
?>
